package com.million.gulimall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.million.gulimall.product.entity.CategoryEntity;
import com.million.gulimall.product.service.CategoryService;
import com.million.common.utils.PageUtils;
import com.million.common.utils.R;


/**
 * 商品三级分类
 *
 * @author wujunjie
 * @email 1658634188@qq.com
 * @date 2022-05-10 21:30:28
 */
@Api(tags = "商品系统：分类维护")
@RestController
@RequestMapping("product/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /**
     * 查询出所有分类以及子分类，用树形结构组装起来，使用递归
     */
    @ApiOperation("查询出所有分类以及子分类，用树形结构组装起来，使用递归")
    @GetMapping("/categoryList/tree")
    public R categoryList () {
        List<CategoryEntity> entities = categoryService.listWithTree();
        return R.ok().put("data", entities);
    }


    /**
     * 信息
     */
    @GetMapping("/categoryInfo/{catId}")
    //@RequiresPermissions("product:category:info")
    public R categoryInfo (@PathVariable("catId") Long catId) {
        CategoryEntity category = categoryService.getById(catId);

        return R.ok().put("category", category);
    }

    /**
     * 保存
     */
    @PostMapping("/addCategory")
    //@RequiresPermissions("product:category:save")
    public R addCategory (@RequestBody CategoryEntity category) {
        categoryService.save(category);

        return R.ok();
    }

    /**
     * 修改
     */
    @PutMapping("/updateCategory")
    //@RequiresPermissions("product:category:update")
    public R updateCategory (@RequestBody CategoryEntity category) {
        categoryService.updateById(category);

        return R.ok();
    }

    /**
     * 删除
     */
    @DeleteMapping("/deleteCategory")
    //@RequiresPermissions("product:category:delete")
    public R deleteCategory (@RequestBody Long[] catIds) {
//        categoryService.removeByIds(Arrays.asList(catIds));
        //1. 检查当前的菜单是否被别的地方使用,删除分类信息
        categoryService.removeMenuByIds(Arrays.asList(catIds));
        return R.ok();
    }

}
